package com.zdp.seckill.controller;

import com.zdp.seckill.common.domain.ResultInfo;
import com.zdp.seckill.common.exception.BusinessException;
import com.zdp.seckill.common.utils.ResultInfoUtil;
import com.zdp.seckill.pojo.vo.ItemModel;
import com.zdp.seckill.pojo.vo.ItemVO;
import com.zdp.seckill.service.ItemService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author sesshomaru
 * @date 2021/6/19 0:19
 */
@RestController
@RequestMapping("item")
public class ItemController extends BaseController{

    @Autowired
    private ItemService itemService;

    @Resource
    private HttpServletRequest httpServletRequest;

    //创建商品的controller
    @RequestMapping(value = "/create",method = {RequestMethod.POST},consumes={CONTENT_TYPE_FORMED})
    @ResponseBody
    public ResultInfo createItem(@RequestParam(name = "title")String title,
                                       @RequestParam(name = "description")String description,
                                       @RequestParam(name = "price") BigDecimal price,
                                       @RequestParam(name = "stock")Integer stock,
                                       @RequestParam(name = "imgUrl")String imgUrl) throws BusinessException {
        //封装service请求用来创建商品
        ItemModel itemModel = new ItemModel();
        itemModel.setTitle(title);
        itemModel.setDescription(description);
        itemModel.setPrice(price);
        itemModel.setStock(stock);
        itemModel.setImgUrl(imgUrl);

        ItemModel itemModelForReturn = itemService.createItem(itemModel);
        ItemVO itemVO = convertVOFromModel(itemModelForReturn);

        return ResultInfoUtil.buildSuccess(httpServletRequest.getRequestURI(), itemVO);
    }

    //商品详情页浏览
    @RequestMapping(value = "/get",method = {RequestMethod.GET})
    @ResponseBody
    public ResultInfo getItem(@RequestParam(name = "id")String id){
        ItemModel itemModel = itemService.getItemById(Integer.valueOf(id));

        ItemVO itemVO = convertVOFromModel(itemModel);

        return ResultInfoUtil.buildSuccess(httpServletRequest.getRequestURI(), itemVO);
    }

    //商品列表页面浏览
    @RequestMapping(value = "/list",method = {RequestMethod.GET})
    @ResponseBody
    public ResultInfo listItem(){
        List<ItemModel> itemModelList = itemService.listItem();

        //使用stream apiJ将list内的itemModel转化为ITEMVO;
        List<ItemVO> itemVOList =  itemModelList.stream().map(itemModel -> {
            ItemVO itemVO = this.convertVOFromModel(itemModel);
            return itemVO;
        }).collect(Collectors.toList());

        return ResultInfoUtil.buildSuccess(httpServletRequest.getRequestURI(), itemVOList);
    }


    private ItemVO convertVOFromModel(ItemModel itemModel){
        if(itemModel == null){
            return null;
        }
        ItemVO itemVO = new ItemVO();
        BeanUtils.copyProperties(itemModel,itemVO);
        if(itemModel.getPromoModel() != null){
            //有正在进行或即将进行的秒杀活动
            itemVO.setPromoStatus(itemModel.getPromoModel().getStatus());
            itemVO.setPromoId(itemModel.getPromoModel().getId());
            itemVO.setPromoPrice(itemModel.getPromoModel().getPromoItemPrice());
            itemVO.setStartDate(itemModel.getPromoModel().getStartDate());
        } else{
            itemVO.setPromoStatus(0);
        }
        return itemVO;
    }

}
